/** ###################################################################
**     Filename  : Events.c
**     Project   : ProcessorExpert
**     Processor : MK40DX256ZVMD10
**     Component : Events
**     Version   : Driver 01.00
**     Compiler  : CodeWarrior ARM C Compiler
**     Date/Time : 2011-07-25, 16:06, # CodeGen: 80
**     Abstract  :
**         This is user's event module.
**         Put your event handler code here.
**     Settings  :
**     Contents  :
**         Counter_OnCounterRestart - void Counter_OnCounterRestart(LDD_TUserData *UserDataPtr);
**         Buzzer_OnCounterRestart  - void Buzzer_OnCounterRestart(LDD_TUserData *UserDataPtr);
**         Cpu_OnNMIINT             - void Cpu_OnNMIINT(void);
**
** ###################################################################*/
/* MODULE Events */

#include "Cpu.h"
#include "Events.h"

/* User includes (#include below this line is not maintained by Processor Expert) */

#define DEBOUNCE_LIMIT 5

extern uint32_t ElapsedTimeMs;
extern uint16_t buzzerCnt;
extern LDD_TDeviceData *tsiDev;
extern LDD_TDeviceData *buzzerDev;
extern void OnButtonPressed(uint8_t Index);

volatile uint8_t ButtonMask = 0;
uint8_t ButtonDebounce[2] = {0, 0};


void DebounceCheck(uint8_t Mask, uint8_t Index) {
  if ((ButtonMask & Mask) != 0x0U) {
	  /* keypress detected */
	if (ButtonDebounce[Index] < DEBOUNCE_LIMIT) {
	  ButtonDebounce[Index]++;
	} else if (ButtonDebounce[Index] == DEBOUNCE_LIMIT){
	  OnButtonPressed(Index);
	  ButtonDebounce[Index]++;
    } else {
    	/* nothing to do, just waiting for the button to be released */
    }
  } else {
	  ButtonDebounce[Index] = 0;  
  }
}

/*
** ===================================================================
**     Event       :  Counter_OnCounterRestart (module Events)
**
**     Component   :  Counter [TimerUnit_LDD]
**     Description :
**         Called if counter overflow/underflow or counter is
**         reinitialized by modulo or compare register matching.
**         OnCounterRestart event and Timer unit must be enabled. See
**         <SetEventMask> and <GetEventMask> methods. This event is
**         available only if a <Interrupt> is enabled.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * UserDataPtr     - Pointer to the user or
**                           RTOS specific data. The pointer passed as
**                           the parameter of Init method.
**     Returns     : Nothing
** ===================================================================
*/
void Counter_OnCounterRestart(LDD_TUserData *UserDataPtr)
{
	ElapsedTimeMs += 1;
}

/*
** ===================================================================
**     Event       :  Buzzer_OnCounterRestart (module Events)
**
**     Component   :  Buzzer [TimerUnit_LDD]
**     Description :
**         Called if counter overflow/underflow or counter is
**         reinitialized by modulo or compare register matching.
**         OnCounterRestart event and Timer unit must be enabled. See
**         <SetEventMask> and <GetEventMask> methods. This event is
**         available only if a <Interrupt> is enabled.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * UserDataPtr     - Pointer to the user or
**                           RTOS specific data. The pointer passed as
**                           the parameter of Init method.
**     Returns     : Nothing
** ===================================================================
*/
void Buzzer_OnCounterRestart(LDD_TUserData *UserDataPtr)
{
	if (--buzzerCnt == 0){
	  Buzzer_Disable(buzzerDev);
	}
}

/*
** ===================================================================
**     Event       :  Cpu_OnNMIINT (module Events)
**
**     Component   :  Cpu [MK40N512MD100]
**     Description :
**         This event is called when the Non maskable interrupt had
**         occurred. This event is automatically enabled when the <NMI
**         interrrupt> property is set to 'Enabled'.
**     Parameters  : None
**     Returns     : Nothing
** ===================================================================
*/
void Cpu_OnNMIINT(void)
{
  /* Write your code here ... */
}

/*
** ===================================================================
**     Event       :  Debounce_OnCounterRestart (module Events)
**
**     Component   :  Debounce [TimerUnit_LDD]
**     Description :
**         Called if counter overflow/underflow or counter is
**         reinitialized by modulo or compare register matching.
**         OnCounterRestart event and Timer unit must be enabled. See
**         <SetEventMask> and <GetEventMask> methods. This event is
**         available only if a <Interrupt> is enabled.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * UserDataPtr     - Pointer to the user or
**                           RTOS specific data. The pointer passed as
**                           the parameter of Init method.
**     Returns     : Nothing
** ===================================================================
*/
void Debounce_OnCounterRestart(LDD_TUserData *UserDataPtr)
{
	  DebounceCheck(1, 0);
	  DebounceCheck(2, 1);
	  ButtonMask = 0;
	  TSI1_TriggerSoftwareScan(tsiDev);
}

/*
** ===================================================================
**     Event       :  TSI1_OnOutOfRange (module Events)
**
**     Component   :  TSI1 [TSI_LDD]
**     Description :
**         This event is enabled only if Out of Range interrupt is
**         selected
**     Parameters  :
**         NAME            - DESCRIPTION
**       * UserDataPtr     - Pointer to the user or
**                           RTOS specific data. This pointer is passed
**                           as the parameter of Init method.
**         mask            - Pin indexes which were detected out of
**                           range.
**     Returns     : Nothing
** ===================================================================
*/
void TSI1_OnOutOfRange(LDD_TUserData *UserDataPtr, TSI1_LDD_TSI_PinMask mask)
{
	if (mask == LDD_TSI_PIN_0_MASK) {
		ButtonMask |= 1;
	} else if (mask == LDD_TSI_PIN_5_MASK){
		ButtonMask |= 2;
	} else {
		/*Do nothing*/
	}
}

/* END Events */

/*
** ###################################################################
**
**     This file was created by Processor Expert 0.00.00 [05.00]
**     for the Freescale Kinetis series of microcontrollers.
**
** ###################################################################
*/
